<?php
/**
 * vanephp web framework
 * 内部视图类
 *
 * @author aray <ofree@qq.com>
 * @link http://www.vanephp.com/
 * @copyright Copyright &copy; 2010 aray
 * @license http://www.vanephp.com/license
 * @package view
 */

if ( ! defined ( 'INVANE' ) )
	exit ( '!' );

/**
 * 内部视图类
 * @author aray
 *
 */
class vane_view_smarty {
	private $smarty;
	private $config;
	private $tplname = '';
	/**
	 * 构造方法
	 */
	public function vane_view_smarty ( ) {
		$this->config = config::get ( 'config', 'view' );
		if ( ! $this->config ) {
			trigger_error ( '请指定SMARTY配置', E_USER_ERROR );
		}
		if ( ! isset ( $this->config['class_path'] ) ) {
			trigger_error ( '请指定SMARTY类路径', E_USER_ERROR );
		}
		if ( ! file_exists ( $this->config['class_path'] ) ) {
			trigger_error ( 'SMARTY类路径' . $this->config['class_path'] . '不存在', E_USER_ERROR );
		}
		require_once $this->config['class_path'];
		$this->smarty = new smarty ( );
		if ( isset ( $this->config['compile_dir'] ) ) {
			$complieDir = $this->config['compile_dir'];
		} else {
			$complieDir = 'complied';
		}
		mkdirs ( $complieDir );
		$this->smarty->compile_dir = $complieDir;
		if ( isset ( $this->config['template_dir'] ) ) {
			$this->smarty->template_dir = $this->config['template_dir'];
		}
		if ( isset ( $this->config['left_delimiter'] ) ) {
			$this->smarty->left_delimiter = $this->config['left_delimiter'];
		}
		if ( isset ( $this->config['right_delimiter'] ) ) {
			$this->smarty->right_delimiter = $this->config['right_delimiter'];
		}
		if ( isset ( $this->config['lifetime'] ) ) {
			$this->smarty->cache_lifetime = $this->config['lifetime'];
		} else {
			$this->smarty->cache_lifetime = 3600;
		}
		if ( isset ( $this->config['cache_dir'] ) ) {
			mkdirs ( $this->config['cache_dir'] );
			$this->smarty->cache_dir = $this->config['cache_dir'];
			$this->smarty->caching = true;
		}
		if ( isset ( $this->config['lifetime'] ) ) {
			$this->smarty->cache_lifetime = $this->config['lifetime'];
		}
		
		# 注册smarty函数
		$this->smarty
			->register_function ( '_url', array (
			& $this, 'func_url' 
		) );
		$this->smarty
			->register_function ( '_get', array (
			& $this, 'func_getChsArrayVariable' 
		) );
		$this->smarty
			->register_function ( '_md5', array (
			& $this, 'func_md5' 
		) );
		$this->smarty
			->register_function ( '_fun', array (
			& $this, 'func_fun' 
		) );
		# 注册smarty修改器
		$this->smarty
			->register_modifier ( '_truncate', array (
			& $this, 'modify_truncate' 
		) );
		
		# 注册smarty变量
		$this->smarty
			->assign ( 'CONTROLLER', CONTROLLER );
		$this->smarty
			->assign ( 'ACTION', ACTION );
		$this->smarty
			->assign ( 'URI', URI );
		$this->smarty
			->assign ( 'NOW', date::now ( ) );
		$this->smarty
			->assign ( 'DATE', date::now ( 'Y-m-d' ) );
		$this->smarty
			->assign ( 'TIME', date::now ( 'H:i:s' ) );
		# 模板名称
		$this->tplname = dispatcher::getController ( );
		# 如果动作为默认动作，优先     控制器名_动作名.tpl (不存在时，使用  控制器名.tpl) 
		if ( dispatcher::getAction ( ) == config::get ( 'default_action' ) ) {
			$firstTpl = $this->smarty->template_dir . DS . $this->tplname . '_' . dispatcher::getAction ( ) . '.tpl';
			if ( file_exists ( $firstTpl ) ) {
				$this->tplname .= '_' . dispatcher::getAction ( ) . '.tpl';
			} else {
				$this->tplname .= '.tpl';
			}
		} else {
			$this->tplname .= '_' . dispatcher::getAction ( ) . '.tpl';
		}
	}
	/**
	 * 获取当前视图对象
	 */
	public function getViewer ( ) {
		return $this;
	}
	/**
	 * 获取模板引擎
	 */
	public function getTpl ( ) {
		return $this->smarty;
	}
	/**
	 * 是否有缓存视图
	 */
	public function isCached ( ) {
		return $this->smarty
			->is_cached ( $this->tplname );
	}
	/**
	 * 渲染模板
	 */
	public function display ( ) {
		if ( ! $this->isCached ( ) ) {
			foreach ( view::$VARS as $k => $v ) {
				$this->smarty
					->assign ( $k, $v );
			}
		}
		$this->smarty
			->display ( $this->tplname );
	}
	
	/***
	 * ==============================================
	 * 注册URL函数
	 * ==============================================
	 */
	
	/**
	 * 注册URL函数
	 * @param array $params
	 */
	public function func_url ( $params ) {
		$controllerName = isset ( $params['controller'] ) ? $params['controller'] : null;
		unset ( $params['controller'] );
		$actionName = isset ( $params['action'] ) ? $params['action'] : null;
		unset ( $params['action'] );
		
		$args = array ();
		foreach ( $params as $key => $value ) {
			if ( is_array ( $value ) ) {
				$args = array_merge ( $args, $value );
				unset ( $params[$key] );
			}
		}
		$args = array_merge ( $args, $params );
		return url ( $controllerName, $actionName, $args );
	}
	
	/**
	 * 注册md5函数
	 * @param array $params
	 */
	public function func_md5 ( $params ) {
		$val = isset ( $params['var'] ) ? $params['var'] : null;
		return md5 ( $val );
	}
	
	/**
	 * 注册调用PHP函数
	 * @param $args
	 */
	function func_fun ( $params ) {
		if ( count ( $params ) > 2 ) {
			$func = $params['fun'];
			unset ( $params['fun'] );
			$var = $params['var'];
			unset ( $params['var'] );
			$ret = call_user_func_array ( $func, $params );
			$this->smarty
				->assign ( $var, $ret );
		}
	}
	
	/**
	 * 获取关联数组中中文名称键的值
	 * @param array $params	
	 */
	function func_getChsArrayVariable ( $params ) {
		$val = isset ( $params['var'][$params['key']] ) ? $params['var'][$params['key']] : null;
		$return = isset ( $params['return'] ) ? $params['return'] : null;
		if ( isset ( $params['return'] ) ) {
			$this->smarty
				->assign ( $params['return'], $val );
		}
		if ( $return ) {
			$this->smarty
				->assign ( $return, $val );
		} else {
			return $val;
		}
	}
	
	/**
	 * 注册truncate修改器
	 * @param string $string	字符串
	 * @param int    $len		长度
	 */
	function modify_truncate ( $string, $len ) {
		return _substr ( $string, 0, $len );
	}
}